//
//  XSVideoWaterPrintView.m
//  AudioVideo
//
//  Created by xueshan1 on 2019/9/4.
//  Copyright © 2019 xueshan1. All rights reserved.
//

#import "XSVideoWaterPrintView.h"

static CGFloat DeleteBtnWidth = 20;

@interface XSVideoWaterPrintView (){
    CGFloat _totalScale;
    CGFloat _maxScale;
    CGFloat _minScale;
    CGPoint _touchStart;

}
@property(nonatomic,assign)VideoWaterPrintType type;

@property(nonatomic,strong)UIView *borderView;
@property (nonatomic, strong) CAShapeLayer *border;
@property(nonatomic,strong)UIButton *deleteBtn;
@property(nonatomic,strong)UIImageView *waterImageView;

@end

@implementation XSVideoWaterPrintView

- (instancetype)initWithFrame:(CGRect)frame waterType:(VideoWaterPrintType)type{
    self = [super initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width + DeleteBtnWidth, frame.size.height + DeleteBtnWidth)];
    if (self) {
        self.type = type;
        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width + DeleteBtnWidth, frame.size.height + DeleteBtnWidth)];
    if (self) {
        self.type = VideoWaterPrintType_Text;
        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    if (self.type == VideoWaterPrintType_Text) {
        [self addSubview:self.borderView];
        [self addSubview:self.contentLab];
    }else{
        [self addSubview:self.waterImageView];
    }

    self.deleteBtn.frame = CGRectMake(self.width - DeleteBtnWidth - 3 , 6, DeleteBtnWidth, DeleteBtnWidth);
    self.deleteBtn.layer.cornerRadius = self.deleteBtn.width/2;
    self.deleteBtn.layer.masksToBounds = YES;
    
    self.hightlight = YES;
    self.textColor = [UIColor redColor];
    _totalScale = 1.0;
    _maxScale = 2;
    _minScale = 0.3;
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes:)] ;
    [self addGestureRecognizer:tapGesture] ;
    
    UIPinchGestureRecognizer *pincheGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGes:)] ;
    [self addGestureRecognizer:pincheGesture] ;
    
    UIRotationGestureRecognizer *rotateGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGes:)] ;
    [self addGestureRecognizer:rotateGesture] ;
    
}

- (UILabel *)contentLab {
    if (!_contentLab) {
        _contentLab = [[UILabel alloc]init];
        _contentLab.frame = CGRectMake(0, DeleteBtnWidth, self.width  - DeleteBtnWidth, self.height - DeleteBtnWidth);
        _contentLab.textAlignment = NSTextAlignmentCenter;
        _contentLab.font = [UIFont systemFontOfSize:WaterLabFont];
        _contentLab.numberOfLines = 0;
        _contentLab.userInteractionEnabled = YES;
        _contentLab.adjustsFontSizeToFitWidth = YES;
    }
    return  _contentLab;
}

- (UIView *)borderView {
    if (!_borderView) {
        _borderView = [[UIView alloc]init];
        _borderView.frame = CGRectMake(0, DeleteBtnWidth, self.width - DeleteBtnWidth, self.height - DeleteBtnWidth);
        _borderView.backgroundColor = [UIColor clearColor];
        
        _border = [CAShapeLayer layer];
        _border.strokeColor = [UIColor redColor].CGColor;
        _border.fillColor = [UIColor clearColor].CGColor;
        _border.path = [UIBezierPath bezierPathWithRect:_borderView.bounds].CGPath;
        _border.frame = _borderView.bounds;
        _border.lineWidth = 2.0;
        //虚线的间隔
        _border.lineDashPattern = @[@8, @4];
        [_borderView.layer addSublayer:_border];
    }
    return _borderView;
}
- (UIImageView *)waterImageView{
    if (!_waterImageView) {
        _waterImageView = [[UIImageView alloc]init];
        _waterImageView.frame = CGRectMake(0, DeleteBtnWidth, self.width - DeleteBtnWidth, self.height - DeleteBtnWidth);
        _waterImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _waterImageView;
}

- (UIButton *)deleteBtn{
    if (!_deleteBtn) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.backgroundColor = [UIColor clearColor];
//        [btn setTitle:@"×" forState:UIControlStateNormal];
//        btn.titleLabel.font = [UIFont systemFontOfSize:20];
//        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"deleteBtnImage.jpeg"] forState:UIControlStateNormal];
        btn.contentMode = UIViewContentModeScaleAspectFit;
        [btn addTarget:self action:@selector(deleteBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        _deleteBtn = btn;
    }
    return _deleteBtn;
}
- (void)deleteBtnClicked:(UIButton *)btn{
    [self removeFromSuperview];
}

- (void)setText:(NSString *)text{
    _text = text;
    self.contentLab.text = text;

}
- (void)setWaterImage:(UIImage *)waterImage{
    _waterImage = waterImage;
    self.waterImageView.image = waterImage;
    
}
- (void)setTextColor:(UIColor *)textColor{
    _textColor = textColor;
    self.contentLab.textColor = textColor;
    self.border.strokeColor = textColor.CGColor;
 
}

- (void)setHightlight:(BOOL)hightlight{
    _hightlight = hightlight;
    self.borderView.hidden = !hightlight;
    self.deleteBtn.hidden = !hightlight;

}
- (void)layoutSubviews{
    [super layoutSubviews];
 
}

#pragma mark - gesture

- (void)tapGes:(UITapGestureRecognizer *)tap{
    if (self.waterDelegate && [self.waterDelegate respondsToSelector:@selector(tapWaterPrintView:)]) {
        [self.waterDelegate tapWaterPrintView:self];
    }
    
    self.hightlight = !self.isHighlight;
    
}
- (void)pinchGes:(UIPinchGestureRecognizer *)pinchGes{
    if (!self.isHighlight) {
        return;
    }
    if (pinchGes.state == UIGestureRecognizerStateBegan) {
        pinchGes.scale = 1.0;
    }else if (pinchGes.state == UIGestureRecognizerStateChanged){
        if (pinchGes.scale > 1.0) {
            if (_totalScale > _maxScale) {
                return;
            }
        }
        if (pinchGes.scale < 1.0) {
            if (_totalScale < _minScale) {
                return;
            }
        }
        
        
        self.transform = CGAffineTransformScale(self.transform,pinchGes.scale, pinchGes.scale);
        _totalScale *= pinchGes.scale;
        pinchGes.scale = 1;
        [self setNeedsDisplay];

    }else{
        
    }
    
//    NSLog(@"缩放%f", pinchGes.scale);

}
- (void)rotationGes:(UIRotationGestureRecognizer *)rotate{
    if (!self.isHighlight) {
        return;
    }
    self.transform = CGAffineTransformRotate(self.transform, rotate.rotation);
    rotate.rotation = 0;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    UITouch *touch = [touches anyObject];
    _touchStart = [touch locationInView:self.superview];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    if (!self.isHighlight) {
        return;
    }
    UITouch *touch = [touches anyObject];
    CGPoint touchMove = [touch locationInView:self.superview];
    CGFloat dealtX = touchMove.x  - _touchStart.x;
    CGFloat dealtY = touchMove.y  - _touchStart.y;
    CGPoint newCenter = CGPointMake(self.centerX + dealtX, self.centerY + dealtY);
    if (CGRectContainsPoint(self.superview.bounds, newCenter)) {
        self.center = newCenter;
    }
    
//    [self translateUsingTouchLocation:touchMove];
    _touchStart = touchMove;

 }

- (void)translateUsingTouchLocation:(CGPoint)touchPoint {
    NSLog(@"%@",NSStringFromCGPoint(touchPoint));
    
    CGPoint newCenter = CGPointMake(self.center.x + touchPoint.x - _touchStart.x,
                                    self.center.y + touchPoint.y - _touchStart.y) ;
    CGFloat midPointX = CGRectGetMidX(self.bounds) ;
    if (newCenter.x > self.superview.bounds.size.width + midPointX - 40) {
        newCenter.x = self.superview.bounds.size.width + midPointX - 40;
    }
    if (newCenter.x < 0 - midPointX + 40)  {
        newCenter.x = -midPointX + 40;
    }
    CGFloat midPointY = CGRectGetMidY(self.bounds);
    if (newCenter.y > self.superview.bounds.size.height + midPointY - 40) {
        newCenter.y = self.superview.bounds.size.height + midPointY - 40;
    }
    if (newCenter.y < -midPointY + 40) {
        newCenter.y = -midPointY + 40;
    }
    self.center = newCenter;
}







@end
